package com.jiepos.mpos.core.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.AppMenuAuthInfDao;
import com.jiepos.mpos.common.dao.MenuButtonDao;
import com.jiepos.mpos.core.util.ContextUtils;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 详细页面显示菜单按钮
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.core.tag 
 * @author 娄伟峰
 * @version 1.0
 * @since 2017年9月23日 下午6:02:31
 * @history（历次修订内容、修订人、修订时间等）
 * @UpdateRemark [说明本次修改内容]
 * @Copyright (c) 2017 江苏杰博实信息技术有限公司
 */
public class ButtonDetailTag extends BodyTagSupport {

	private static final long serialVersionUID = 7366326665143937358L;

	private final Logger log = LoggerFactory.getLogger(ButtonDetailTag.class);

	/**
	 * 菜单编码
	 */
	private String menuCode;
 
	/**
	 * 菜单代码
	 */
	private String buttonCode;
	/**
	 * form id
	 */
	private String formId;
	/**
	 * 是否隐藏标签中的返回按钮
	 */
	private boolean hideBack;
	@Override
	public int doEndTag() {
	 
		WebApplicationContext wac = WebApplicationContextUtils
				.getWebApplicationContext(pageContext.getServletContext());
 		AppMenuAuthInfDao menuAuthDao = (AppMenuAuthInfDao) wac.getBean("appMenuAuthInfDao"); 
		MenuButtonDao menuButtonDao = (MenuButtonDao) wac.getBean("menuButtonDao");

		HttpSession session = pageContext.getSession();
		// 获取当前登陆用户的角色id
		String roleId = String.valueOf(session.getAttribute(SystemConstant.SESSION_ROLE_UUID));
		// 根据当前登陆用户的角色id和当前菜单id查询此角色的权限。
		String sql = "select * from " + Table.T_APP_MENUAUTH_INF + " where role_uuid=? ";
		sql = sql + " and menu_uuid=? ";
		sql = sql + " and " + SystemConstant.REC_ST + "!=? ";
 
		List paramList = new ArrayList();
		paramList.add(roleId);
		paramList.add(menuCode);
		paramList.add(SystemConstant.DEL_STATUS);
		List<Map> menuAuthList = menuAuthDao.queryForList(sql,paramList);
		String returnContent = "";
		if (menuAuthList != null && menuAuthList.size() > 0) {
			Map menuAuthMap = menuAuthList.get(0);
			String auth_buttons = String.valueOf(menuAuthMap.get("AUTH_BUTTONS"));
			//log.debug("auth_buttons==" + auth_buttons);
			if (!StringUtils.isBlank(auth_buttons)) {
				// 如果当前角色的当前操作不为空，则根据buttonIds查询出这些菜单所有的操作
				String[] sqlAuthButtonIds = auth_buttons.split(",");
				StringBuffer buffer = new StringBuffer();  
				for(int i = 0 ; i < sqlAuthButtonIds.length ; i++){
					 buffer.append("?, ");  
				}
				buffer.deleteCharAt(buffer.length() - 1);  
				buffer.deleteCharAt(buffer.length() - 1);  
				StringBuilder menuButtonSql = new StringBuilder("select *  from " + Table.T_APP_MENUBUTTON_INF);
				menuButtonSql.append(" where button_uuid in ("+buffer.toString()+") and  ");
				menuButtonSql.append(SystemConstant.REC_ST + "!=? " );
				menuButtonSql.append(" and button_type=? ");
				menuButtonSql.append(" order by button_orders ");
				Map menuButtonMap = null;
				List paramList2 = new ArrayList();
				for(int i = 0 ; i < sqlAuthButtonIds.length ; i++){
					paramList2.add(sqlAuthButtonIds[i]);
				}
				paramList2.add(SystemConstant.DEL_STATUS);
				paramList2.add("2");
				List<Map> menuButtonList = menuButtonDao.queryForList(menuButtonSql.toString(),paramList2);

				for (int i = 0; i < menuButtonList.size(); i++) {
					menuButtonMap = menuButtonList.get(i);
					String tempButtonCode = StringUtils.objToString(menuButtonMap.get("BUTTON_CODE"));
					String buttonName = StringUtils.objToString(menuButtonMap.get("BUTTON_NAME"));
					String buttonUrl = StringUtils.objToString(menuButtonMap.get("BUTTON_URL"));
					String functionName = StringUtils.objToString(menuButtonMap.get("FUNCTION_NAME"));
					String buttonIconcls = StringUtils.objToString(menuButtonMap.get("BUTTON_ICONCLS"));
					if (!StringUtils.isBlank(returnContent)) {
						returnContent = returnContent + " ";
					}
					String newFunctionName = "";
					String appName = ContextUtils.getAppName();
					if(!StringUtils.isBlank(appName)){
						appName = "/" + ContextUtils.getAppName() ;
					}
				 
					if (!StringUtils.isBlank(functionName)) {
						newFunctionName = functionName + "('" + appName + buttonUrl + "','"+formId+"')";
					}
					if(StringUtils.isBlank(buttonCode) || tempButtonCode.equals(buttonCode)){
						returnContent = returnContent + "<a href=\"javascript:void(0)\"   id='wwww'  class=\"easyui-linkbutton btn detail-remove\"   iconCls=\""+buttonIconcls+"\"     onclick=\""
								+ newFunctionName + "\">"+buttonName+"</a>&nbsp;&nbsp;" ;
					}
					
					
 				}
				if(!hideBack){
					returnContent = returnContent+"<a href=\"javascript:void(0)\" class=\"easyui-linkbutton btn\" iconCls=\"icon-back\" onclick=\"back()\">返回</a>";
				}

			}
		}
	 
		JspWriter writter = pageContext.getOut();
	//	log.debug("Generated code by Button tag : " + returnContent);
		try {
			writter.print(returnContent + "  ");
		} catch (IOException e) {
			log.error("buttonOperate: build error.");
		}
		return EVAL_PAGE;
	}
	public String getMenuCode() {
		return menuCode;
	}
	public void setMenuCode(String menuCode) {
		this.menuCode = menuCode;
	}
	public String getButtonCode() {
		return buttonCode;
	}
	public void setButtonCode(String buttonCode) {
		this.buttonCode = buttonCode;
	}
	public String getFormId() {
		return formId;
	}
	public void setFormId(String formId) {
		this.formId = formId;
	}

	public boolean isHideBack() {
		return hideBack;
	}
	public void setHideBack(boolean hideBack) {
		this.hideBack = hideBack;
	}


}
